<?php
class SockHttp
	{
	//CONNECTION INFORMATION
		private $connection;
		private $port;
		private $host;
		private $timeout;
		private $scheme;

	//ERRORS
		private $errors;

	//MISC
		private $cookie;
		private $referer;
		private $userAgent;

	//FUNCTIONS
		function SockHttp($ho,$po=80,$sc="tcp://", $to = 30)
		{
			$this->port    = $po;
			$this->host    = $ho;
			$this->timeout = $to;
			$this->scheme  = $sc;

			$this->errors  = array();

			$this->cookie  = array();
			$this->referer = array();
			$this->cookie  = array();
		}

		function connect()
		{
			$this->connection = fsockopen($this->scheme.$this->host,$this->port,$err1,$err2,$this->timeout);
			$this->errors[]=$err1;
			$this->errors[]=$err2;
		}

		function close()
		{
			//we don't clean error ...
			fclose($this->connection);
			$this->connection="";
		}

		function get($path,$headers=array())
		{
			if(!$this->connection)
				$this->connect();

			$request =  "GET ".trim($path)." HTTP/1.1\r\n";
			$request .= "Host: ".$this->host."\r\n";
			foreach($headers as $header)
				$request .= $header."\r\n";
			$request .= "Connection: Close\r\n\r\n";

			fwrite($this->connection,$request);

			$ret = array();
			$ind = false;
			$redirection = "";

			while(!feof($this->connection))
			{
				$tmp = fgets($this->connection);

				if( preg_match("#^[-A-Za-z]+:.*$#i",$tmp) || !$ind )
				{
					if( preg_match("#^Location: (.*)$#i",$tmp,$url) )
						$redirection = $url[1];
					$ret["HEAD"][] = $tmp;
				}
				else
					$ret["BODY"][] = $tmp;
				$ind = true;
			}
			//better to close connection here
			$this->close();
			if($redirection)//recursivity powaaaa!
				return $this->get($redirection);
			else
				return $ret;
		}

		function post($path,$var,$content_body="",$headers=array(),$ct="application/x-www-form-urlencoded")
		{
			if(!$this->connection)
				$this->connect();

			//cleanup path to avoid bad request (between https and http in case of an opened connection known as ssl)
			//first check if it's on the same domain
			if(preg_match("#^https?://".$this->host."/.*$#i",$path))
				$path=preg_replace("#^https?://".$this->host."#i","",$path);

			$content="";
			foreach($var as $key=>$contenu)
				$content .= $key."=".urlencode($contenu)."&";

			$request =  "POST ".trim($path)." HTTP/1.1\r\n";
			$request .= "Host: ".$this->host."\r\n";
			$request .= "Content-type: ".$ct."\r\n";
			foreach($headers as $header)
				$request .= $header."\r\n";
			$request .= "Content-Length: ".(strlen($content)+strlen($content_body))."\r\n";
			$request .= "Connection: Close\r\n\n";
			//need an empty line to begin post

			//in case both are empty no \r\n that will return 400
			if($content)
				$request .= substr($content,0,-1)."\r\n";
			if($content_body)
				$request .= $content_body."\r\n";

			//echo "<br />".$request."<br />";

			fwrite($this->connection,$request."\r\n");

			$ret = array();
			$ind = false;
			$redirection = "";

			while(!feof($this->connection))
			{
				$tmp = fgets($this->connection);

				if( preg_match("#^[-A-Za-z]+:.*$#i",$tmp) || !$ind )
				{
					if( preg_match("#^Location: (.*)$#i",$tmp,$url) )
						$redirection = $url[1];
					$ret["HEAD"][] = $tmp;
				}
				else
					$ret["BODY"][] = $tmp;
				$ind = true;
			}
			//better to close connection here if we want to reopen another one in case of redirection
			$this->close();
			if($redirection)//recursivity powaaaa!
			{
				//echo "<br />REDIRECTION : ".$redirection."<br />";
				return $this->post($redirection,$var,$content_body,$headers,$ct);
			}
			else
				return $ret;
		}
	}
?>